package client.gui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import client.logic.Facade;
import client.logic.GuiFacade;
import encription.SecurityHandler;

/**
 *	a panel that shows a login screen , giving the option to login with
 *	a user name and a password , or open a registeration window
 */
public class pnlLoginScreen extends JPanel{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField txtName;
	private JButton btnRegister;
	private JLabel lblWelcome;
	private JLabel lblPassword;
	private JLabel lblName;
	private JPasswordField txtPassword;
	private JButton btnLogin;

	/**
	 * Creates an instance of pnlLogin
	 */
	public pnlLoginScreen(){
		initGUI();
	}
	
	/**
	 * initializes the GUI
	 */
	private void initGUI() {
		try {
			{
				setLayout(null);
				{
					btnLogin = new JButton();
					add(btnLogin);
					btnLogin.setText("Login");
					btnLogin.setBounds(144, 179, 69, 22);
					btnLogin.addActionListener(new ActionListener(){
						public void actionPerformed(ActionEvent e){
							Facade.getInstance().login(txtName.getText(), txtPassword.getPassword());
						}
					});
				}
				{
					txtName = new JTextField();
					add(txtName);
					txtName.setBounds(127, 96, 128, 20);
				}
				{
					txtPassword = new JPasswordField();
					add(txtPassword);
					txtPassword.setBounds(127, 128, 128, 20);
				}
				{
					lblName = new JLabel();
					add(lblName);
					lblName.setText("Name:");
					lblName.setBounds(58, 96, 69, 14);
				}
				{
					lblPassword = new JLabel();
					add(lblPassword);
					lblPassword.setText("Password:");
					lblPassword.setBounds(58, 129, 69, 14);
				}
				{
					lblWelcome = new JLabel();
					add(lblWelcome);
					lblWelcome.setText("Welcome to our server!!!");
					lblWelcome.setBounds(88, 33, 162, 24);
				}
				{
					btnRegister = new JButton();
					this.add(btnRegister);
					btnRegister.setText("Register");
					btnRegister.setBounds(250, 180, 98, 21);
					btnRegister.addActionListener(new ActionListener(){
						public void actionPerformed(ActionEvent e){
							GuiFacade.getInstance().showRegister();
						}
					});
				}
			}
			{
				this.setSize(398, 294);
				this.setPreferredSize(new java.awt.Dimension(398, 246));
			}
		} catch(Exception e) {
			SecurityHandler.catchException("initializing the login screen", e, false);
			//e.printStackTrace();
		}
	}

}